package com.jd.pet.service.impl;

import com.jd.basic.service.BaiduAiAuditService;
import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.basic.util.AjaxResult;
import com.jd.basic.util.LoginContext;
import com.jd.org.domain.Employee;
import com.jd.org.mapper.EmployeeMapper;
import com.jd.pet.domain.Pet;
import com.jd.pet.domain.PetDetail;
import com.jd.pet.domain.PetOnlineAuditLog;
import com.jd.pet.mapper.PetDetailMapper;
import com.jd.pet.mapper.PetMapper;
import com.jd.pet.mapper.PetOnlineAuditLogMapper;
import com.jd.pet.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements PetService {


    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private BaiduAiAuditService baiduAiAuditService;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;



    @Override
    @Transactional
    public void save(Pet pet) {
        // 保存宠物信息
        super.save(pet);
        // 保存宠物明细
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail!=null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {
        // 删除宠物明细
        petDetailMapper.deleteByPetId(id);
        // 删除宠物信息
        super.del(id);
    }

    @Override
    @Transactional
    public void update(Pet pet) {
        // 修改宠物信息
        super.update(pet);
        // 修改宠物明细
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail!=null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.update(petDetail);
        }
    }


    @Override
    @Transactional
    public void patchRemove(List<Long> ids) {
        petDetailMapper.patchDeleteByPetId(ids);
        // 批量删除宠物信息
        super.patchRemove(ids);
    }


    @Override
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
        // 上架
        for (Long id : ids) {
            // 上架自动审核文本
            Pet pet = petMapper.loadById(id);
            Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());
            // 审核图片
            Boolean boo2 = baiduAiAuditService.imageAudit(pet.getResources());

            // 公共的信息
            PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
            auditLog.setPetId(id);

            // 获取审核人
            Long logininfoId = LoginContext.getLogininfo(request).getId();
            Employee employee = employeeMapper.loadByLogininfoId(logininfoId);
            auditLog.setAuditId(employee.getId());

            // 审核通过
            if (boo1 && boo2) { // 文本和图片都审核通过
                // 审核通过：该状态和时间+记录审核日志，状态改为1，上架
                petMapper.onsale(new Date(),id);

                auditLog.setState(1); // 表示审核成功
                auditLog.setNote("审核成功了哦！");
                petOnlineAuditLogMapper.save(auditLog);
            }else {
                //审核失败-note-当前是下架状态，如果审核失败！
                //记录审核日志
                auditLog.setState(0);
                auditLog.setNote("审核失败,宠物名称或图片不合法!!!");
                petOnlineAuditLogMapper.save(auditLog);
                return AjaxResult.me().setSuccess(false).setMsg("审核失败，图文、文本非法，或图片不存在！");
            }
        }
        return new AjaxResult();
    }

    @Override
    public void offsale(List<Long> ids, HttpServletRequest request) {
        // 操作数据库：改下架时间=系统时间，改状态为0下架
        petMapper.offsale(new Date(),ids);

    }
}
